package com.dragon.util;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;

public class MailUtil implements Runnable {
	private String email;// 鏀朵欢浜洪偖绠�
	private String code;// 婵�娲荤爜

	public MailUtil(String email, String code) {
		this.email = email;
		this.code = code;
	}
	
	public void run(){
		// 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        String from = "showluu@163.com";// 发件人电子邮箱
        String host = "smtp.163.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)

        Properties properties = System.getProperties();// 获取系统属性

        properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
        properties.setProperty("mail.smtp.auth", "true");// 打开认证

        try {
            //QQ邮箱需要下面这段代码，163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);


            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("showluu@163.com", "showlu19970108xx"); // 发件人邮箱账号、授权码
                }
            });
            session.setDebug(true);
            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3设置邮件主题
            message.setSubject("账号激活");
            String content = "<html><head></head><body><h1>点击连接激活账号</h1><h3><a href='http://localhost:8080/RegisterDemo/ActiveServlet?code="+
					code + "'>http://localhost:8080/RegisterDemo/ActiveServlet?code=" + code+
					"</a></h3></body></html>";
			message.setContent(content, "text/html;charset=UTF-8");
            // 2.4设置邮件内容
//            String content = "您的验证码是："+code;
//            message.setContent(content, "text/html;charset=UTF-8");
//            message.setText(content);
            // 3.发送邮件
            Transport.send(message);
            System.out.println("邮件成功发送!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	public static void main(String[] args) {
		new MailUtil("1309617271@qq.com", "1212").run();
	}

//	public void run() {
//		// 1.鍒涘缓杩炴帴瀵硅薄javax.mail.Session
//		// 2.鍒涘缓閭欢瀵硅薄 javax.mail.Message
//		// 3.鍙戦�佷竴灏佹縺娲婚偖浠�
//		String from = "xxx@qq.com";// 鍙戜欢浜虹數瀛愰偖绠�
//		String host = "smtp.qq.com"; // 鎸囧畾鍙戦�侀偖浠剁殑涓绘満smtp.qq.com(QQ)|smtp.163.com(缃戞槗)
//
//		Properties properties = System.getProperties();// 鑾峰彇绯荤粺灞炴��
//
//		properties.setProperty("mail.smtp.host", host);// 璁剧疆閭欢鏈嶅姟鍣�
//		properties.setProperty("mail.smtp.auth", "true");// 鎵撳紑璁よ瘉
//
//		try {
//			//QQ閭闇�瑕佷笅闈㈣繖娈典唬鐮侊紝163閭涓嶉渶瑕�
//			MailSSLSocketFactory sf = new MailSSLSocketFactory();
//			sf.setTrustAllHosts(true);
//			properties.put("mail.smtp.ssl.enable", "true");
//			properties.put("mail.smtp.ssl.socketFactory", sf);
//			
//			
//			// 1.鑾峰彇榛樿session瀵硅薄
//			Session session = Session.getDefaultInstance(properties, new Authenticator() {
//				public PasswordAuthentication getPasswordAuthentication() {
//					return new PasswordAuthentication("xxx@qq.com", "xxx"); // 鍙戜欢浜洪偖绠辫处鍙枫�佹巿鏉冪爜
//				}
//			});
//
//			// 2.鍒涘缓閭欢瀵硅薄
//			Message message = new MimeMessage(session);
//			// 2.1璁剧疆鍙戜欢浜�
//			message.setFrom(new InternetAddress(from));
//			// 2.2璁剧疆鎺ユ敹浜�
//			message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//			// 2.3璁剧疆閭欢涓婚
//			message.setSubject("璐﹀彿婵�娲�");
//			// 2.4璁剧疆閭欢鍐呭
//			String content = "<html><head></head><body><h1>杩欐槸涓�灏佹縺娲婚偖浠�,婵�娲昏鐐瑰嚮浠ヤ笅閾炬帴</h1><h3><a href='http://localhost:8080/RegisterDemo/ActiveServlet?code="
//					+ code + "'>http://localhost:8080/RegisterDemo/ActiveServlet?code=" + code
//					+ "</href></h3></body></html>";
//			message.setContent(content, "text/html;charset=UTF-8");
//			// 3.鍙戦�侀偖浠�
//			Transport.send(message);
//			System.out.println("閭欢鎴愬姛鍙戦��!");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
